package sz.szsmk.citizencard.ui.newversion.fragment.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.LayoutHelper
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.bumptech.glide.Glide
import com.gyf.immersionbar.ImmersionBar
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.layout_homeusermodule.view.*
import kotlinx.android.synthetic.main.layout_oneplustwo.view.*
import kotlinx.android.synthetic.main.layout_oneplustwo.view.iv_img
import sz.szsmk.citizencard.R
import sz.szsmk.citizencard.model.network.clarkbean.response.AdInfo
import sz.szsmk.citizencard.util.GlideRoundTransform
import sz.szsmk.citizencard.util.JumpThirdView

/**
 *  author : hyt
 *  date : 2020/10/29
 *  description :
 */
class OnePlusTwoAdapter(var mContext: Context, var layoutHelper: LayoutHelper)
    : DelegateAdapter.Adapter<OnePlusTwoAdapter.OnePlusTwoViewHolder>() {
    //这里 一拖二的布局
    var datas = mutableListOf<AdInfo>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)=
            OnePlusTwoViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_oneplustwo, parent, false))

    override fun onBindViewHolder(holder: OnePlusTwoViewHolder, position: Int) {
        // only vertical
        holder.itemView.layoutParams =
                VirtualLayoutManager.LayoutParams(
                        VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400))

        var bean = datas[position]
            Glide.with(mContext).load(bean.resource_url)
                .transform(GlideRoundTransform(mContext))
                .into(holder.itemView.iv_img)

        if(position==0){
            var layoutParams = holder.itemView.iv_img.layoutParams as ViewGroup.MarginLayoutParams
            layoutParams.setMargins(0,0,15,0)
            holder.itemView.iv_img.layoutParams = layoutParams
        }else{
            var layoutParams = holder.itemView.iv_img.layoutParams as ViewGroup.MarginLayoutParams
            layoutParams.setMargins(0,0,4,0)
            holder.itemView.iv_img.layoutParams = layoutParams
        }

        holder.itemView.iv_img.setOnClickListener {
            JumpThirdView.baseToJumpView(mContext,bean.link_url,bean.sys_label, bean.need_header,"")
        }

    }

    override fun getItemCount() = datas.size

    override fun onCreateLayoutHelper() = layoutHelper

    fun addDatas(datas: List<AdInfo>){
        this.datas.clear()
        this.datas.addAll(datas)
        notifyDataSetChanged()
    }

    class OnePlusTwoViewHolder(view: View): RecyclerView.ViewHolder(view)
}